package com.pj.project4sp.sp_map;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.pj.project4sp.SP;
import com.pj.project4sp.sp_map_log.Json;
import com.pj.project4sp.sp_map_log.SpMapLog;
import com.pj.project4sp.sp_map_log.SpMapLogMapper;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Controller: sp_map -- 场景表
 * @author lh 
 */
@RestController
@RequestMapping("/SpMap/")
//@SaCheckLogin
public class SpMapController {

	/** 底层 Mapper 对象 */
	@Autowired
	SpMapMapper spMapMapper;
	@Autowired
	SpMapLogMapper spMapLogMapper;

	/** 增 */  
	@RequestMapping("add")
//	@SaCheckPermission(SpMap.PERMISSION_CODE)
	@Transactional(rollbackFor = Exception.class)
	public AjaxJson add(@RequestBody SpMap s){
		s.setCreateTime(new Date());
		spMapMapper.add(s);
		s = spMapMapper.getById(SP.publicMapper.getPrimarykey());
		ex("新增",""+s);
		return AjaxJson.getSuccessData(s);
	}



	/** 删 */  
	@RequestMapping("delete")
//	@SaCheckPermission(SpMap.PERMISSION_CODE)
	public AjaxJson delete(Long id){
		int line = spMapMapper.delete(id);
		ex("删除","id:"+id);
		return AjaxJson.getByLine(line);
	}
	
	/** 删 - 根据id列表 */  
	@RequestMapping("deleteByIds")
//	@SaCheckPermission(SpMap.PERMISSION_CODE)
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class); 
		int line = SP.publicMapper.deleteByIds(SpMap.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}
	
	/** 改 */  
	@RequestMapping("update")
//	@SaCheckPermission(SpMap.PERMISSION_CODE)
	public AjaxJson update(@RequestBody SpMap s){
		SpMap ss = spMapMapper.getById(s.getId());
		s.setUpdateTime(new Date());
		int line = spMapMapper.update(s);
		Json json=new Json();
		json.setId(s.getId());
		json.setName("修改后");
		json.setModelJson(s.getModelJson());
		json.setSceneJson(s.getSceneJson());
		JSONObject jsona = JSONUtil.parseObj(json, false,true);
		Json json1=new Json();
		json1.setId(s.getId());
		json1.setName("修改前");
		json1.setModelJson(ss.getModelJson());
		json1.setSceneJson(ss.getSceneJson());
		JSONObject jsona1 = JSONUtil.parseObj(json1, false,true);
		ArrayList<JSONObject> list = CollUtil.newArrayList(jsona, jsona1);
		JSONArray jsonArray = JSONUtil.parseArray(list);
		ex("修改", String.valueOf(jsonArray));
		return AjaxJson.getByLine(line);
	}


	@RequestMapping("updateName")
	public AjaxJson updateName(@RequestBody SpMap s){
		SpMap ss = spMapMapper.getById(s.getId());
		s.setUpdateTime(new Date());
		int line = spMapMapper.updateName(s);
		ex("修改","修改后+id:"+s.getId()+"name:"+s.getName()
				+"修改前+id:"+s.getId()+"name:"+ss.getName());
		return AjaxJson.getByLine(line);
	}

	/** 查 - 根据id */  
	@RequestMapping("getById")
	public AjaxJson getById(Long id){
		SpMap s = spMapMapper.getById(id);
		return AjaxJson.getSuccessData(s);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	@RequestMapping("getList")
	public AjaxJson getList() { 
		SoMap so = SoMap.getRequestSoMap();
		List<SpMap> list = spMapMapper.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}
	

	public void ex(String name,String content){
		SpMapLog spMapLog = new SpMapLog();
		spMapLog.setName(name);
		spMapLog.setContent(content);
		spMapLog.setCreateTime(new Date());
		spMapLogMapper.add(spMapLog);
	}
	
	
	
	

}
